<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

require_once $mainframe->getPath("class", "com_leagueman");

$t				= intval($params->get( "team", 0 ) );
$show_picture	= intval($params->get( "show_picture", 0) );
$projects		= $params->get( "projects", "" );
$delay			= intval($params->get( "delay", 0 ) );
$date_format	= $params->get( "date_format", "%A, <br/>%d.%m.%Y %H:%M Uhr");

$tm = intval(mosGetParam($_GET, "tm", 0));
if($tm){
	
	$teammap	= LM::getLMTable("Teammap");
	$teammap->load($tm);
	$projects	= $teammap->teammap_project;
	$t			= $teammap->teammap_team;
}

if($t > 0){
	
	$template = LM::getTemplate();
		
	$where[] = sprintf("(match_team1 = %s OR match_team2 = %s)", $t, $t);
	$where[] = "match_date > DATE_ADD(CURDATE(), INTERVAL $delay DAY)";
	if(!empty($projects)){
		$where[] = "match_project IN ($projects)";
	}
	
	$row = null;
	
	$database->setQuery("SELECT match_team1, match_team2, match_date, match_time
						   FROM #__leagueman_match
						  WHERE" . implode(" AND ", $where) . " LIMIT 1");
	$database->loadObject($row);
	
	if($row){
		
		$match_date_time = $row->match_date . " " . $row->match_time;
	
		$team1 = LM::getLMTable("Team");
		$team2 = LM::getLMTable("Team");
	
		$team1->load($row->match_team1);
		$team2->load($row->match_team2);
	
		$query1 = sprintf("SELECT * FROM #__leagueman_match
							WHERE match_team1 = %s AND match_team2 = %s
							  AND published = 1", $team1->getID(), $team2->getID());
	
		$database->setQuery($query1);
		$matches = $database->loadObjectList();
	
		$query2 = sprintf("SELECT * FROM #__leagueman_match
							WHERE match_team2 = %s AND match_team1 = %s
							  AND published = 1", $team1->getID(), $team2->getID());
						  
		$database->setQuery($query2);
	
		$matches = array_merge($matches, $database->loadObjectList());
	
		$s = $u = $n = 0;
	
		foreach($matches as $match){
			if($match->match_team1 == $t){
				if($match->match_result_0_1 > $match->match_result_0_2) $s++;
				if($match->match_result_0_1 == $match->match_result_0_2) $u++;
				if($match->match_result_0_1 < $match->match_result_0_2) $n++;
			}
		
			if($match->match_team2 == $t){
				if($match->match_result_0_2 > $match->match_result_0_1) $s++;
				if($match->match_result_0_2 == $match->match_result_0_1) $u++;
				if($match->match_result_0_2 < $match->match_result_0_1) $n++;
			}
		}
	
		ob_start();
		?>
		
			<table width="100%">
				<tr>
					<td colspan="3" align="center">
						<b><?php echo mosFormatDate($match_date_time, $date_format); ?></b><br/>
					</td>
				</tr>
				<tr>
					<td colspan="3" align="center"><?php echo $team1->getName(); ?></td>
				</tr>
<?php   if($show_picture){?>
				<tr>
					<td width="49%" align="center" valign="middle"><?php $template->plugin("thumb", "logos", $team1->getLogo(), 56, 56);?></td>
					<td width="2%"><b>vs.</b></td>
					<td width="49%" align="center" valign="middle"><?php $template->plugin("thumb", "logos", $team2->getLogo(), 56, 56);?></td>
				</tr>
<?php	} else { ?>
				<tr>
					<td colspan="3" align="center"><b>vs</b></td>
				</tr>   
<?php	} ?>
				<tr>
					<td colspan="3" align="center"><?php echo $team2->getName(); ?></td>
				</tr>
				<tr>
					<td colspan="3" align="center">
						<b><?php echo _LM_HISTORY; ?></b><br/>
						<?php printf("%s / %s / %s ( %s )", $s, $u, $n, _LM_W_D_L); ?><br/>
					</td>
				</tr>
			</table>
<?php
			$html = ob_get_contents();
		ob_end_clean();
	
		echo $html;
	
	} else {
		echo "<div align=\"center\">no next match available</div>";
	}
}
?>